package com.cloud.position.xlctawler.dao;


import com.cloud.position.xlctawler.entity.JobInfoDto;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DataPipline implements Pipeline {
    @Override
    public void process(ResultItems resultItems, Task task) {
        //获取封装号的招聘详情对象
        JobInfoDto jobInfo = resultItems.get("JobInfo");
        //判断数据是否不为空
        if(jobInfo != null){
            //保存自数据库中
            Connection conn;
            //注册驱动
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                //获取数据库连接对象 Connection
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/midapp?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC","root","root123");
                //定义sql
                String sql="insert into job_info (company_name,company_addr,company_info,job_name,job_info,salary_min,salary_max,url,time,address) " +
                        "values(?,?,?,?,?,?,?,?,?,?);";

                //给?赋值
                PreparedStatement pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,jobInfo.getCompanyName());
                pstmt.setString(2,jobInfo.getCompanyAddr());
                pstmt.setString(3,jobInfo.getCompanyInfo());
                pstmt.setString(4,jobInfo.getJobName());
                pstmt.setString(5,jobInfo.getJobInfo());
                pstmt.setInt(6,jobInfo.getSalaryMin());
                pstmt.setInt(7,jobInfo.getSalaryMax());
                pstmt.setString(8,jobInfo.getUrl());
                pstmt.setString(9,jobInfo.getTime());
                pstmt.setString(10,"长沙");
                //执行sql，接受返回结果
                int count = pstmt.executeUpdate();
                //处理结果
                System.out.println(count);
                //释放资源
                conn.close();
                pstmt.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
